package com.Ngbussiness.server;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.Ngbussiness.dao.yongjinDao;
import com.Ngbussiness.dao.impl.yongjinDaoImpl;
import com.Ngbussiness.util.Calenda;

public class uploadServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		System.out.println("weishenme");
	   yongjinDao yj=new yongjinDaoImpl();
	   String temp = request.getSession().getServletContext().getRealPath("/")
			+ "temp"; // 临时目录
     	System.out.println("temp=" + temp);
		String loadpath ="E:/wamp/www/Ngbussiness/image/busphoto/";
		System.out.println("loadpath=" + loadpath);
		DiskFileUpload fu = new DiskFileUpload();
		fu.setSizeMax(1 * 1024 * 1024); // 设置允许用户上传文件大小,单位:字节
		fu.setSizeThreshold(4096); // 设置最多只允许在内存中存储的数据,单位:字节
		fu.setRepositoryPath(loadpath); // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
		// 开始读取上传信息
		int index = 0;
		List fileItems = null;
		System.out.println("111");
		try {
			fileItems = fu.parseRequest(request);
			System.out.println(fileItems);
			System.out.println("fileItems=" + fileItems);
			System.out.println("fileitmeli");
		} catch (Exception e) {
			e.printStackTrace();
		}
        System.out.println("222");
        Iterator iter = null;
      //  System.out.println("***"+iter.toString());
        try {
        	 iter = fileItems.iterator(); // 依次处理每个上传的文件
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("error");
		}
        boolean b1=false;
		
		System.out.println("***"+iter.toString());
		while (iter.hasNext()) {
			FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息
			if (!item.isFormField()) {
//				String name = item.getName();// 获取上传文件名,包括路径
				Calenda c=new Calenda();
				long time=(long)c.getNowTime().getTime();
	            String time1=""+time;
				System.out.println("333");
				long size = item.getSize();
				index++;
				File fNew = new File(loadpath,time1+".png");
				try {
					item.write(fNew);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("error");
				}
				out.print("image/busphoto/"+time1+".png");
				System.out.println("image/busphoto/"+time1+".png");

			} else // 取出不是文件域的所有表单信息
			{
				String fieldvalue = item.getString();
				// 如果包含中文应写为：(转为UTF-8编码)
				// String fieldvalue = new
				// String(item.getString().getBytes(),"UTF-8");
			}
		}
		
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
             doGet(request,response);
	}

}
